package org.vaadinpa.gui;

import org.vaadinpa.gui.util.NotificationUtil;

import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

public class CreatePlayerLayout extends VerticalLayout implements ClickListener {

	private static final long serialVersionUID = 4736772759717021366L;
	private TextField planetField;
	private TextField rulerField;

	public CreatePlayerLayout() {

		addStyleName(Reindeer.LAYOUT_BLACK);

		Label l = new Label("Your browser is not registered as a player");
		addComponent(l);

		rulerField = new TextField("Ruler name");
		rulerField.focus();
		addComponent(rulerField);

		planetField = new TextField("Planet name");
		addComponent(planetField);

		Button register = new Button("Register");
		register.setClickShortcut(KeyCode.ENTER);
		register.addListener(this);
		addComponent(register);
		setExpandRatio(register, 1);

		setSpacing(true);
		setMargin(true);
		setSizeFull();
	}

	public void buttonClick(ClickEvent event) {
		VaadinPA current = VaadinPA.getCurrent();

		try {
			current.createNewPlayer(rulerField.getValue().toString(), planetField.getValue().toString());
		} catch (IllegalArgumentException e) {
			NotificationUtil.notify("invalid rulername or planet name", "");
			return;
		}
		current.showMainLayout();
	}
}
